
goog.provide('esn.FriendSurveyList');

goog.require('esn.FriendSurvey');
goog.require('esn.soy.friendSurveyList');
goog.require('goog.array');
goog.require('goog.ui.Component');


/**
 * @constructor
 */
esn.FriendSurveyList = function(appContext, opt_friends, opt_domHelper) {
  goog.base(this, opt_domHelper);

  this.appContext_ = appContext;

  this.surveyClient_ = this.appContext_.getService(
      esn.constants.ServiceId.SURVEY_CLIENT);

  this.friends_ = opt_friends || [];
};
goog.inherits(esn.FriendSurveyList, goog.ui.Component);

esn.FriendSurveyList.EventType = {
  FRIEND_ADDED: goog.events.getUniqueId('fa'),
  FRIEND_REMOVED: goog.events.getUniqueId('fr')
};

esn.FriendSurveyList.prototype.createDom = function() {  
  goog.base(this, 'createDom');

  this.setElementInternal(soy.renderAsElement(esn.soy.friendSurveyList.table));

  this.body_ = this.getElementByClass('body');
};

esn.FriendSurveyList.prototype.enterDocument = function() {  
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this,
      esn.FriendSurvey.EventType.REMOVE_CLICK,
      this.handleRemoveFriendClick_);
};

/**
 * @param {esn.model.StudentModel|esn.model.FriendSurveyModel} friend
 */
esn.FriendSurveyList.prototype.addFriend = function(model, opt_saveOnAdd) {
  if (model instanceof esn.model.StudentModel) {
    model = new esn.model.FriendSurveyModel(model,  this.surveyClient_);
  }

  // Only add a the friend if it hasn't already been added.
  var friendSurvey = null;
  if (!this.getChild(String(model.getFriend().getId()))) {
    friendSurvey = new esn.FriendSurvey(this.appContext_, model,
        this.getDomHelper());
    this.addChild(friendSurvey);
    friendSurvey.render(this.body_);
    this.dispatchEvent(esn.FriendSurveyList.EventType.FRIEND_ADDED);

    if (opt_saveOnAdd) {
      friendSurvey.getDao().save();
    }
  }

  return friendSurvey;
};

/**
 * @param {Array.<esn.model.StudentModel|esn.model.FriendSurveyModel>} friends
 */
esn.FriendSurveyList.prototype.addFriends = function(models, opt_saveOnAdd) {
  var friendSurveys = goog.array.map(models, function(model) {
    return this.addFriend(model);
  }, this);

  if (opt_saveOnAdd) {
    var friendSurveyModels = [];
    goog.array.forEach(friendSurveys, function(friendSurvey) {
     // A friend survery can be null if it was previously added.
      if (friendSurvey) {
        friendSurveyModels.push(friendSurvey.getModel());
      }
    }, this);
    this.surveyClient_.updateFriendSurveys(
        friendSurveyModels,
        goog.bind(this.updateFriendSurveysResponse_, this));
  }

  return friendSurveys;
};

esn.FriendSurveyList.prototype.updateFriendSurveysResponse_ = function(
    response) {
  if (!response.hasError()) {
    goog.array.forEach(response.getData(), function(friendSurveyModel) {
      var friendSurvey = this.getChild(
          String(friendSurveyModel.getFriend().getId()));
      if (friendSurvey) {
        friendSurvey.getModel().mergeFrom(friendSurveyModel);
      }
    }, this);
  } else {
    window.console.log(response.getError().getInternalMessage());
  }
};

esn.FriendSurveyList.prototype.validateCompletion = function() {
  this.forEachChild(function(friendSurvey) {
    friendSurvey.validateCompletion();
  });
};

esn.FriendSurveyList.prototype.handleRemoveFriendClick_ = function(e) {
  var friendSurvey = e.target;
  this.removeChild(friendSurvey.getId(), true);

  // Delete this friend survey from the database.
  friendSurvey.getDao().remove();
  friendSurvey.dispose();

  this.dispatchEvent(esn.FriendSurveyList.EventType.FRIEND_REMOVED);
};
